package com.baymax.test.scene.helper

import android.app.Activity
import android.app.Fragment
import android.app.FragmentManager
import android.util.Log
import androidx.activity.OnBackPressedDispatcherOwner
import androidx.activity.addCallback
import androidx.fragment.app.FragmentManager.OnBackStackChangedListener
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.bytedance.scene.NavigationSceneUtility
import com.bytedance.scene.navigation.NavigationListener
import com.bytedance.scene.navigation.NavigationScene

class TF : Fragment()
fun Activity.setupSceneAnonymous(): NavigationScene {
    val ac = this
    val TAG = "setupSceneAnonymous"
    Log.d(TAG, "tryOpenScene launchWhenResumed $this")
    val nav = NavigationSceneUtility.setupWithActivity(
        this, EmptyHolderScene::class.java
    ).tag("__SceneContainerCompact_tmp").drawWindowBackground(false).supportRestore(true)
        .build().navigationScene
    return requireNotNull(nav) { "can not ini rootNavigationScene" }.apply {
        Log.w(
            TAG,
            "call back in dispatcher ..." + (ac is OnBackPressedDispatcherOwner)
        )
        val ac: Activity = this@setupSceneAnonymous
        if (ac is OnBackPressedDispatcherOwner) {
            val dispatcher =
                (ac as OnBackPressedDispatcherOwner).onBackPressedDispatcher
            dispatcher.addCallback {
                Log.w(TAG, "call back in" + dispatcher)
                val popped = nav.onBackPressed()
                isEnabled = popped
                if (!popped) {
                    dispatcher.onBackPressed()
                    isEnabled = true
                }
            }
        } else {
            var previous = 0
            val l :FragmentManager.OnBackStackChangedListener = FragmentManager.OnBackStackChangedListener {
                println("backStackChange ${ac.fragmentManager.backStackEntryCount}")
                val c = ac.fragmentManager.backStackEntryCount
                if (c < previous) {
                    nav.onBackPressed()
                }
                previous = c
            }
            ac.fragmentManager.addOnBackStackChangedListener(l)
            nav.addNavigationListener(nav, NavigationListener { from, to, isPush ->
                Log.d(TAG, "navigationChange $from ,$to ,$isPush")
                ac.fragmentManager.apply {
                    if (isPush && from != null) {
                        val bt = beginTransaction()
                        val toString = to.toString()
                        bt.add(TF(), toString)
                        bt.addToBackStack(toString)
                        bt.commit()
                    }
                }
            })
           nav.lifecycle.addObserver(object: DefaultLifecycleObserver {
               override fun onDestroy(owner: LifecycleOwner) {
                   super.onDestroy(owner)
                   ac.fragmentManager.removeOnBackStackChangedListener(l)
               }
           })
        }
    }


}
